﻿$(function () {
    $("#rent-grid").datagrid({
        url: $.Api("House/Rent/Grid"),
        nowrap: false, rownumbers: true, border: false,
        fit: true, singleSelect: true,
        pageSize: 20, pagination: true,
        idField: "ID", toolbar: "#rent-tools",
        sortName: "UpdateTime", sortOrder: "desc",
        columns:
        [[
            { field: "Name", title: "新闻标题", width: 400, align: "center", sortable: true },
            { field: "InsertTime", title: "添加时间", width: 140, align: "center", sortable: true },
            { field: "UpdateTime", title: "更新时间", width: 140, align: "center", sortable: true },
            {
                field: "Top", title: "顶置", width: 50, align: "center", sortable: true,
                formatter: function (value, row, index) { return $.GetYesOrNo(value); }
            }
        ]]
    });

    $("#rent-dictionary-tree").tree({
        lines: true, url: $.Api("Sys/Dictionary/FilterTree&tag=Category"),
        onSelect: function (node) {
            $("#rent-grid").datagrid("clearChecked");
            $("#rent-grid").datagrid("load", { dictionaryid: node.id });
        }
    });

    $("#rent-searchbox").searchbox({
        width: 150, prompt: "请输入新闻标题查询",
        searcher: function (value) {
            $("#rent-grid").datagrid("clearChecked");
            $("#rent-grid").datagrid("load", { search: value });
        }
    });
});

var Rent = {
    //添加
    Insert: function () {
        var node = $("#rent-dictionary-tree").tree("getSelected");
        if (node) {
            Rent.Mutual("添加", "house/rent/Edit.aspx?did=" + node.id);
        }
        else { Rent.Mutual("添加", "house/rent/Edit.aspx"); }
    },
    //修改
    Update: function () {
        var row = $("#rent-grid").datagrid("getSelected");
        if (row) {
            Rent.Mutual("修改", "house/rent/Edit.aspx?id=" + row.ID);
        }
        else { $.EasyuiSlide(nullrowmsg); }
    },
    //删除
    Delete: function () {
        var row = $("#rent-grid").datagrid("getSelected");
        if (row) {
            $.EasyuiConfirm("确认删除?", function (del) {
                if (del) {
                    $.AjaxMask({
                        url: $.Api("House/Rent/Delete"),
                        type: "post", data: { id: row.ID },
                        success: function (result) {
                            result = JSON.parse(result);
                            $.EasyuiSlide(result.Message);
                            if (result.Success) { Rent.Reload(); }
                        }
                    });
                }
            });
        }
        else { $.EasyuiSlide(delnullrowmsg); }
    },
    //实体操作
    Mutual: function (title, href) {
        $.ModelDialog({
            title: title, href: href, width: 668,
            handler: function () {
                $.ModelForm({
                    validate: true, button: $(this),
                    editor: "#rent-content",
                    id: "#rent-model",
                    url: $.Api("House/Rent/Save"),
                    success: function (result) { Rent.Reload(); }
                });
            },
            onLoad: function () { Rent.FormInit(); }
        });
    },
    //弹窗初始化
    FormInit: function () {
        $("#rent-dictionaryid").combotree({
            width: 236, panelHeight: "auto",
            url: $.Api("Sys/Dictionary/Tree&tag=Category")
        });
        $.EditorInit("#rent-content", 200, "p", true);
    },

    //预览
    Preview: function () {
        var row = $("#rent-grid").datagrid("getSelected");
        if (row) {
            window.open("../Rent.aspx?id=" + row.ID);
        }
        else { $.EasyuiSlide(nullrowmsg); }
    },

    //顶置
    Top: function () {
        var row = $("#rent-grid").datagrid("getSelected");
        if (row) {
            $.AjaxMask({
                url: $.Api("House/Rent/Top"),
                type: "post", data: { id: row.ID },
                success: function (result) {
                    result = JSON.parse(result);
                    $.EasyuiSlide(result.Message);
                    if (result.Success) { Rent.Reload(); }
                }
            });
        }
        else { $.EasyuiSlide(nullrowmsg); }
    },

    //刷新列表
    Reload: function () {
        $.DataGridReload("#rent-grid");
    },

    //展开部门
    DicTreeRedo: function () {
        $("#rent-dictionary-tree").tree("expandAll");
    },
    //收起部门
    DicTreeUndo: function () {
        $("#rent-dictionary-tree").tree("collapseAll");
    },
    //查看全部
    DicAll: function () {
        var node = $("#rent-dictionary-tree").tree("getRoot");
        $("#rent-dictionary-tree").tree("select", node.target);
        $("#rent-grid").datagrid("clearChecked");
        $("#rent-grid").datagrid("load", { dictionaryid: node.id });
    }
};